package persentation.presentUniversityDean;

import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import vo.userVO.TeacherVO;
import businesslogic.courseBl.CourseController;
import businesslogic.userBl.UniversityDeanController;

public class TeacherInfoPanel extends JPanel {
	private MyTabbedPane tabbedPane;
	private JTable table;
	private ArrayList<TeacherVO> teacherVOs;

	private UniversityDeanController uDeanController;
	private String[] departmentList = new String[29];
	private CourseController courseController;

	/**
	 * Create the panel.
	 * @throws Exception 
	 */
	public TeacherInfoPanel(UniversityDeanController uDeanController) {
		setLayout(null);
		this.uDeanController = uDeanController;
		try {
			teacherVOs = uDeanController.getTeacherList();
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		courseController = new CourseController(uDeanController.getUserID());
		ArrayList<String> departmentArrayList = null;
		try {
			departmentArrayList = courseController
					.getAllDepartments();
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
			ExceptionHandler.networkException();
		}
		departmentList[0] = "全部";
		for (int i = 0; i < departmentArrayList.size(); i++) {
			departmentList[i + 1] = departmentArrayList.get(i);
		}

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(92, 76, 606, 347);
		add(scrollPane);

		table = new JTable();
		table.setModel(new DefaultTableModel(new Object[][] {
				{ null, null, null }, { null, null, null },
				{ null, null, null }, { null, null, null },
				{ null, null, null }, { null, null, null },
				{ null, null, null }, { null, null, null },
				{ null, null, null }, { null, null, null },
				{ null, null, null }, { null, null, null },
				{ null, null, null }, }, new String[] { "\u6559\u5DE5\u53F7",
				"\u59D3\u540D", "\u6240\u5C5E\u9662\u7CFB" }) {
			Class[] columnTypes = new Class[] { String.class, String.class,
					String.class };

			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}

			boolean[] columnEditables = new boolean[] { false, false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(2).setResizable(false);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		scrollPane.setViewportView(table);

		final JComboBox comboBox = new JComboBox();
		// comboBox.setModel(new DefaultComboBoxModel(new String[] {"软件学院",
		// "计算机系", "物理系", "匡亚明学院", "商学院"}));
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					changeStudentList(departmentList[comboBox
							.getSelectedIndex()]);
				}
			}
		});
		comboBox.setModel(new DefaultComboBoxModel(departmentList));
		comboBox.setBounds(166, 27, 173, 33);
		add(comboBox);

		JLabel label = new JLabel("院系");
		label.setBounds(102, 27, 36, 33);
		add(label);
		fillTable();
	}

	public void fillTable() {

		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		tableModel.setRowCount(0);
		for (TeacherVO teacher : teacherVOs) {
			String[] array = new String[3];
			array[0] = teacher.getId();
			array[1] = teacher.getName();
			array[2] = teacher.getDepartmentId();

			System.out.println(array[0]);
			System.out.println(array[1]);
			System.out.println(array[2]);
			tableModel.addRow(array);
			table.invalidate();
			table.updateUI();
		}
	}

	public void changeStudentList(String departmentID) {
		if (departmentID.equals("全部")) {
			try {
				teacherVOs = uDeanController.getTeacherList();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			try {
				teacherVOs = uDeanController.getTeacherList(departmentID);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		fillTable();
	}
}
